package elion.com.theElion.util;

import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class ReflectUtil {
    /**
     * @param obj 需要获取字段的类的对象
     * @return 对象的字段名数组
     */
    public static String[] getFieldsNames(Object obj) {
        Field[] attrs = obj.getClass().getDeclaredFields();
        String[] names = getNames(attrs);
        return names;
    }


    /**
     * @param className 需要获取属性的字段
     * @return 类的字段名数组
     */

    public static String[] getFieldsNames(String className) {
        try {
            Field[] attrs = Class.forName(className).getDeclaredFields();
            String[] names = getNames(attrs);
            return names;
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    /**
     * @param attrs 字段数组
     * @return 类的字段名数组
     *  @exception android studio instant run
     *      会导致出现两个未定义的参数名 $change serialVersionUID
     */
    private static String[] getNames(Field[] attrs) {
        String[] names = new String[attrs.length];
        boolean flag = false;
        int i = 0;
        for (Field f : attrs) {
            if(f.getName().equals("serialVersionUID")) {
                flag = true;
                continue;
            }
            if(f.isSynthetic())
                continue;
            names[i] = f.getName();
            i++;
        }
        if(flag) {
            names = Arrays.copyOf(names,names.length-2);
        }
        return names;
    }



    /**
     * @param obj  要被调用get函数的obj
     * @param name 要get什么字段？
     * @return get到的字段数据！
     */
    public static String useGetMethod(Object obj, String name) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = obj.getClass().getMethod("get" + captureString(name).trim(), new Class[]{});
        String result = "" + method.invoke(obj, new Object[]{});
        return result;
    }

    /**
     * @param obj  要被调用get函数的obj
     * @param name 要get什么字段？
     * @return get到的字段数据！
     */
    public static String useGetClassMethod(Object obj, String name) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = obj.getClass().getMethod(captureString(name).trim(), new Class[]{});
        String result = "" + method.invoke(obj, new Object[]{});
        return result;
    }

    /**
     * @param obj  要被调用get函数的obj
     * @param name 要set什么字段？
     */
    public static void useSetMethod(Object obj, String name, String arg) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = obj.getClass().getMethod("set" + captureString(name).trim(), String.class);
        String result = "" + method.invoke(obj, arg);
    }

    public static String captureString(String string) {
        char[] cs = string.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }
}
